package com.sx.manage.service.function;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.model.constants.type.MUrlType;
import com.sx.model.manage.mybatis.vo.function.MFuncButtonVO;
import com.sx.model.manage.mybatis.vo.function.MFunctionInfoVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;

/**
 * 模块与按钮功能service
 * @author lenovo
 *
 */
@Service("functionInfoWebService")
public class FunctionInfoWebService {
	/**
	 * 日志
	 */
	private Logger _log = LoggerFactory.getLogger(this.getClass());

	///模块按钮
	@Resource(name="mBaseFunctionButtonInfoServiceImpl")
	private IMBaseFunctionButtonInfoService mBaseFunctionButtonInfoServiceImpl;

    ///模块信息
	@Resource(name="mBaseFunctionInfoServiceImpl")
	private IMBaseFunctionInfoService mBaseFunctionInfoServiceImpl;
	public Logger get_log() {
		return _log;
	}
	public void set_log(Logger _log) {
		this._log = _log;
	}
	/**
	 * 根据url获取 请求的类型
	 * @return
	 */
	public ResultObjectVO<MUrlType> getMUrlTypeByUrl(String url){
		if( StringUtils.isBlank( url ) ){
			_log.debug(" getMUrlTypeByUrl 参数为空，直接返回");
			return new ResultObjectVO<>("m.parameter.is.null");
		}
		
		_log.debug(" getMUrlTypeByUrl 根据url 获取请求类型url={} 首先查询是不是按钮信息",url);
		ResultObjectVO<MUrlType>  result = null;
		MUrlType type = null;
		_log.debug("");
		ResultObjectVO<MFuncButtonVO>  buttonResult  = this.mBaseFunctionButtonInfoServiceImpl.queryButtonByUrl(url);
		if( buttonResult.isSuccess() && null != buttonResult.getObjVo() ){
			_log.debug("请求是按钮类型url={}",url);
			type = buttonResult.getObjVo().getRequest_type();
			
			result = new ResultObjectVO<>();
			result.setObjVo( type );
			
			return result ;
		}
		  //至少留下两个/，保证是到了controller/method一级。这样权限出问题几率会小很多
		  if( StringUtils.countMatches(url,"/")>2 ){
			  //截取在匹配
			  url = url.substring(0,url.lastIndexOf("/") );
			  _log.debug("截取后的 url="+url);
				  //递归调用自己
				  return getMUrlTypeByUrl(url);
		  }
		_log.debug("按钮为空，再查询模块信息url={}",url);
		 ResultObjectVO<MFunctionInfoVO> functionResult = this.mBaseFunctionInfoServiceImpl.get_functionByUrl(url);
		 if( functionResult.isSuccess() && null != functionResult.getObjVo() ){
			 _log.debug("是模块信息url={}",url);
			 if( url.equals( functionResult.getObjVo().getFunction_address() )){
				 type = MUrlType.MODEL_AND_VIEW_URL;
			 }else if(url.equals( functionResult.getObjVo().getFunction_url() )){
				 type = MUrlType.AXAJ_URL; 
			 }
			 result = new ResultObjectVO<>();
			result.setObjVo( type );
				
			return result ;
		 }
		 _log.debug("没有根据url查询到数据url={}",url);
		return new  ResultObjectVO<>("m.data.is.null");
	
	}
}
